package fictional.GUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * Creates the first window which shows up when the program is
 * run. It prompts the user for connection details in order
 * to connection to the DB.
 */
@SuppressWarnings("serial")
public class ConnectionSetupPopup extends javax.swing.JFrame 
{

	Login login;
	public static final String DEFAULT_CONNECTION_URL = "jdbc:mysql://mysqlsrv.cs.tau.ac.il:3306/DbMysql13";
	public static final String DEFAULT_USERNAME = "DbMysql13";
	public static final String DEFAULT_PASSWORD = "DbMysql13";

    public ConnectionSetupPopup(Login login) 
    {
    	this(login, DEFAULT_CONNECTION_URL, DEFAULT_USERNAME, DEFAULT_PASSWORD);
    }
    
    public ConnectionSetupPopup(Login login, String connectionString, String connectionUsername, String connectionPassword)  
    {
        this.login = login;
        
        this.setSize(650, 400);
		this.setResizable(false);
		this.setTitle("Connection Parameters");
		this.setIconImage(new ImageIcon(getClass().getResource("resources/windowIcon.gif")).getImage());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        initFrame();
        
        connectionURLTextField.setText(connectionString);
        usernameTextField.setText(connectionUsername);
        passwordTextField.setText(connectionPassword);
    }

    private void initFrame() {

        mainPanel = new javax.swing.JPanel();
        titleLabel = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        usernameLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        connectionURLLabel = new javax.swing.JLabel();
        usernameTextField = new javax.swing.JTextField();
        passwordTextField = new javax.swing.JTextField();
        connectionURLTextField = new javax.swing.JTextField();
        
        java.awt.GridBagConstraints cons;        
        mainPanel.setLayout(new java.awt.GridBagLayout());
        mainPanel.setBackground(new Color(252, 251, 251));

        titleLabel.setText("PLEASE SET CONNECTION PARAMETERS"); 
        titleLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
        cons = new java.awt.GridBagConstraints();
        cons.gridx = 0;
        cons.gridy = 0;
        cons.insets = new Insets(10, 20, 0, 10);
        cons.gridwidth = 2;
        cons.anchor = java.awt.GridBagConstraints.WEST;
        mainPanel.add(titleLabel, cons);
        
        connectionURLLabel.setText("Connection URL: ");
        cons = new java.awt.GridBagConstraints();
        cons.gridy = 1;
        cons.anchor = java.awt.GridBagConstraints.WEST;
        cons.insets = new Insets(10, 20, 0, 10);
        mainPanel.add(connectionURLLabel, cons);
        
        usernameLabel.setText("Username: ");
        cons.gridy = 2;
        mainPanel.add(usernameLabel, cons);
        
        passwordLabel.setText("Password: ");
        cons.gridy = 3;
        mainPanel.add(passwordLabel, cons);
        
        cons = new java.awt.GridBagConstraints();
        cons.gridx = 1;
        cons.gridy = 1;
        cons.fill = GridBagConstraints.HORIZONTAL;
        cons.weightx = 1.0;
        cons.insets = new java.awt.Insets(10, 0, 0, 20);
        mainPanel.add(connectionURLTextField, cons);

        cons.gridy = 2;
        mainPanel.add(passwordTextField, cons);

        cons.gridy = 3;
        mainPanel.add(usernameTextField, cons);
        
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new GridBagLayout());

        okButton.setText("Connect"); 
        okButton.setMaximumSize(new java.awt.Dimension(85, 23));
        okButton.setMinimumSize(new java.awt.Dimension(85, 23));
        okButton.setPreferredSize(new java.awt.Dimension(85, 23));
        okButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                okButtonMouseClicked(evt);
            }
        });
        
        cons = new java.awt.GridBagConstraints();
        cons.gridx = 0;
        cons.gridy = 0;
		cons.insets = new java.awt.Insets(10, 30, 10, 30);
        cons.anchor = GridBagConstraints.WEST;
        cons.fill = GridBagConstraints.HORIZONTAL;
        buttonsPanel.add(okButton, cons);

        cancelButton.setText("Exit"); 
        cancelButton.setMaximumSize(new java.awt.Dimension(85, 23));
        cancelButton.setMinimumSize(new java.awt.Dimension(85, 23));
        cancelButton.setPreferredSize(new java.awt.Dimension(85, 23));
        cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelButtonMouseClicked(evt);
            }
        });
        
        cons.gridy = 0;
        cons.gridx = 1;
        buttonsPanel.add(cancelButton, cons);
        
        cons.gridx = 0;
        cons.gridy = 4;
        cons.gridwidth = 2;
        cons.fill = GridBagConstraints.HORIZONTAL;
        cons.insets = new java.awt.Insets(10, 0, 10, 0);
        buttonsPanel.setBackground(new Color(252, 251, 251));
        mainPanel.add(buttonsPanel, cons);
        
        this.add(mainPanel);

        pack();
    }

    private void okButtonMouseClicked(java.awt.event.MouseEvent evt) 
    {
    	if (connectionURLTextField.getText().isEmpty() || usernameTextField.getText().isEmpty() || passwordTextField.getText().isEmpty())
    	{
    		JOptionPane.showMessageDialog(null, "Please make sure to fill all fields marked with *.", "Error Setting Up Connection Parameters", JOptionPane.ERROR_MESSAGE);
    		return;
    	}
    	
    	login.setConnectionParamaters(connectionURLTextField.getText(), usernameTextField.getText(), passwordTextField.getText());
    	this.setVisible(false);
    	this.dispose();
    	return;
    }

    private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt) 
    {
    	login.exit();
        this.setVisible(false);
        this.dispose();
    }
    
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel usernameLabel;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JLabel connectionURLLabel;
    private javax.swing.JTextField usernameTextField;
    private javax.swing.JTextField passwordTextField;
    private javax.swing.JTextField connectionURLTextField;
}